{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "5b8930c0",
   "metadata": {},
   "source": [
    "# 实践：宿舍人脸识别门禁系统\n",
    ">1.创建FaceSet\n",
    ">2.查询FaceSet\n",
    ">3.存入face_token数据\n",
    ">4.获取FaceSet信息数据\n",
    ">5.人脸搜索\n",
    ">6.人脸对比"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "27d3dd47",
   "metadata": {},
   "source": [
    "## 创建FaceSet"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "911b6288",
   "metadata": {},
   "source": [
    ">描述\n",
    ">* 创建一个人脸的集合 FaceSet，用于存储人脸标识 face_token。一个 FaceSet 能够存储10000个 face_token。\n",
    ">*试用API Key可以创建1000个FaceSet，正式API Key可以创建10000个FaceSet。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4982e0df",
   "metadata": {},
   "source": [
    ">调用URL\n",
    ">*https://api-cn.faceplusplus.com/facepp/v3/faceset/create"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "412e1e1b",
   "metadata": {},
   "source": [
    ">调用方法：POST"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ded1a866",
   "metadata": {},
   "source": [
    "> 请求参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "3fc896cf",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "f639bece",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>是否必选</th>\n",
       "      <th>参数名</th>\n",
       "      <th>类型</th>\n",
       "      <th>参数说明</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>必选</td>\n",
       "      <td>api_key</td>\n",
       "      <td>String</td>\n",
       "      <td>调用此 API 的 API Key</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>必选</td>\n",
       "      <td>api_secret</td>\n",
       "      <td>String</td>\n",
       "      <td>调用此 API 的 API Secret</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>可选</td>\n",
       "      <td>display_name</td>\n",
       "      <td>String</td>\n",
       "      <td>人脸集合的名字，最长256个字符，不能包括字符^@,&amp;=*'\"</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>可选</td>\n",
       "      <td>outer_id</td>\n",
       "      <td>String</td>\n",
       "      <td>账号下全局唯一的 FaceSet 自定义标识，可以用来管理 FaceSet 对象。最长255...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>可选</td>\n",
       "      <td>tags</td>\n",
       "      <td>String</td>\n",
       "      <td>FaceSet 自定义标签组成的字符串，用来对 FaceSet 分组。最长255个字符，多个...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>可选</td>\n",
       "      <td>face_tokens</td>\n",
       "      <td>String</td>\n",
       "      <td>人脸标识 face_token，可以是一个或者多个，用逗号分隔。最多不超过5个 face_t...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>可选</td>\n",
       "      <td>user_data</td>\n",
       "      <td>String</td>\n",
       "      <td>自定义用户信息，不大于16 KB，不能包括字符^@,&amp;=*'\"</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>可选</td>\n",
       "      <td>force_merge</td>\n",
       "      <td>Int</td>\n",
       "      <td>在传入 outer_id 的情况下，如果 outer_id 已经存在，是否将 face_to...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  是否必选           参数名      类型  \\\n",
       "0   必选       api_key  String   \n",
       "1   必选    api_secret  String   \n",
       "2   可选  display_name  String   \n",
       "3   可选      outer_id  String   \n",
       "4   可选          tags  String   \n",
       "5   可选   face_tokens  String   \n",
       "6   可选     user_data  String   \n",
       "7   可选   force_merge     Int   \n",
       "\n",
       "                                                参数说明  \n",
       "0                                  调用此 API 的 API Key  \n",
       "1                               调用此 API 的 API Secret  \n",
       "2                    人脸集合的名字，最长256个字符，不能包括字符^@,&=*'\"  \n",
       "3  账号下全局唯一的 FaceSet 自定义标识，可以用来管理 FaceSet 对象。最长255...  \n",
       "4  FaceSet 自定义标签组成的字符串，用来对 FaceSet 分组。最长255个字符，多个...  \n",
       "5  人脸标识 face_token，可以是一个或者多个，用逗号分隔。最多不超过5个 face_t...  \n",
       "6                    自定义用户信息，不大于16 KB，不能包括字符^@,&=*'\"  \n",
       "7  在传入 outer_id 的情况下，如果 outer_id 已经存在，是否将 face_to...  "
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.read_html(\"\"\"<div class=\"table-wrap\"><table class=\"wrapped confluenceTable\"><colgroup><col><col><col><col></colgroup><tbody><tr><th class=\"confluenceTh\"><p><span style=\"color: rgb(0,0,0);\">是否必选</span></p></th><th class=\"confluenceTh\"><p><span style=\"color: rgb(0,0,0);\">参数名</span></p></th><th class=\"confluenceTh\"><p><span style=\"color: rgb(0,0,0);\">类型</span></p></th><th class=\"confluenceTh\"><p><span style=\"color: rgb(0,0,0);\">参数说明</span></p></th></tr><tr><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">必选</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">api_key</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">String</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">调用此 API 的 API Key</span></p></td></tr><tr><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">必选</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">api_secret</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">String</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">调用此 API 的 API Secret</span></p></td></tr><tr><td class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">可选</span></td><td class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">display_name</span></td><td class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">String</span></td><td class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">人脸集合的名字，最长<span class=\"inline-comment-marker\" data-ref=\"9f098728-ac30-41a2-aab6-05b79f8a7d2e\">256</span>个字符，不能包括字符^@,&=*'\"</span></td></tr><tr><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">可选</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">outer_id</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">String</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">账号下全局唯一的 FaceSet 自定义标识，可以用来管理 FaceSet 对象。最长255个字符，<span class=\"inline-comment-marker\" data-ref=\"63fc0fd1-cd44-4256-bab5-971a939d66e2\"><span class=\"inline-comment-marker\" data-ref=\"183dcb11-c22c-47f4-aa9c-2ba6f3c8210c\">不能包括字符^@,&=*'\"</span></span></span></td></tr><tr><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">可选</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">tags</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">String</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">FaceSet 自定义标签组成的字符串，用来对 FaceSet 分组。最长<span class=\"inline-comment-marker\" data-ref=\"1be760b6-f20d-4ff3-a086-b7f763abd84d\">255</span>个字符，多个 tag 用逗号分隔，每个 tag 不能包括字符^@,&=*'\"</span></td></tr><tr><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">可选</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">face_tokens</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">String</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">人脸标识 face_token，可以是一个或者多个，用逗号分隔。最多不超过5个 face_token</span></p></td></tr><tr><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">可选</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">user_data</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">String</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">自定义用户信息，不大于<span class=\"inline-comment-marker\" data-ref=\"f9edadb1-691d-4d80-933e-909096780f61\">16 KB，不能包括字符^@,&=*'\"</span></span></td></tr><tr><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">可选</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">force_merge</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">Int</span></td><td colspan=\"1\" class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">在传入 outer_id 的情况下，如果 outer_id 已经存在，是否将 face_token 加入已经存在的 FaceSet 中</span></p><p><span style=\"color: rgb(0,0,0);\">0：不将 face_tokens 加入已存在的 FaceSet 中，直接返回 FACESET_EXIST 错误</span></p><p><span style=\"color: rgb(0,0,0);\">1：将 face_tokens 加入已存在的 FaceSet 中</span></p><p><span style=\"color: rgb(0,0,0);\">默认值为0</span></p></td></tr></tbody></table></div>\"\"\")[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "8be547b5",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "a53a54e8",
   "metadata": {},
   "outputs": [],
   "source": [
    "API_key = 'b0EHP47Q3skXaAdxRUxVSPdsDFpQe93z'\n",
    "API_Secret = 'ep0nGCCMAqSrGeXMx7202O6Xy7MDC1k7'\n",
    "\n",
    "FaceSet_url = 'https://api-cn.faceplusplus.com/facepp/v3/faceset/create'\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_Secret\n",
    "}\n",
    "r = requests.post(url = FaceSet_url,params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "1116dddf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "52703d80",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': 'ec1cd2a82d1f83953e6bc146316bd3ef',\n",
       " 'time_used': 171,\n",
       " 'face_count': 0,\n",
       " 'face_added': 0,\n",
       " 'request_id': '1648884542,23e930cd-d7e8-429d-977f-04c75d9ee6a0',\n",
       " 'outer_id': '',\n",
       " 'failure_detail': []}"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "e8c527f3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'ec1cd2a82d1f83953e6bc146316bd3ef'"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "faceset_token = r.json()['faceset_token']\n",
    "faceset_token"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "523dff2f",
   "metadata": {},
   "source": [
    "## 2.查询人脸库"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "03d8a44e",
   "metadata": {},
   "source": [
    "> 描述\n",
    ">* 获取某一 API Key 下的 FaceSet 列表及其 faceset_token、outer_id、display_name 和 tags 等信息。\n",
    ">* 注意：2017年8月16日后，调用本接口将不会一次性返回全量的 FaceSet。单次查询最多返回 100 个 FaceSet。如需获取全量数据，需要配合使用 start 和 next 参数。请尽快修改调整您的程序。\n",
    "* 调用URL\n",
    ">* https://api-cn.faceplusplus.com/facepp/v3/faceset/getdetail"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "1b885180",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>是否必选</th>\n",
       "      <th>参数名</th>\n",
       "      <th>类型</th>\n",
       "      <th>参数说明</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>必选</td>\n",
       "      <td>api_key</td>\n",
       "      <td>String</td>\n",
       "      <td>调用此API的API Key</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>必选</td>\n",
       "      <td>api_secret</td>\n",
       "      <td>String</td>\n",
       "      <td>调用此API的API Secret</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>可选</td>\n",
       "      <td>tags</td>\n",
       "      <td>String</td>\n",
       "      <td>包含需要查询的FaceSet标签的字符串，用逗号分隔</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>可选</td>\n",
       "      <td>start</td>\n",
       "      <td>Int</td>\n",
       "      <td>一个数字 n，表示开始返回的 faceset_token 在传入的 API Key 下的序号...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  是否必选         参数名      类型                                               参数说明\n",
       "0   必选     api_key  String                                     调用此API的API Key\n",
       "1   必选  api_secret  String                                  调用此API的API Secret\n",
       "2   可选        tags  String                         包含需要查询的FaceSet标签的字符串，用逗号分隔\n",
       "3   可选       start     Int  一个数字 n，表示开始返回的 faceset_token 在传入的 API Key 下的序号..."
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "pd.read_html(\"\"\"<div class=\"table-wrap\"><table class=\"wrapped confluenceTable\"><colgroup><col><col><col><col></colgroup><tbody><tr><th class=\"confluenceTh\"><p><span style=\"color: rgb(0,0,0);\">是否必选</span></p></th><th class=\"confluenceTh\"><p><span style=\"color: rgb(0,0,0);\">参数名</span></p></th><th class=\"confluenceTh\"><p><span style=\"color: rgb(0,0,0);\">类型</span></p></th><th class=\"confluenceTh\"><p><span style=\"color: rgb(0,0,0);\">参数说明</span></p></th></tr><tr><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">必选</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">api_key</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">String</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">调用此API的API Key</span></p></td></tr><tr><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">必选</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">api_secret</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">String</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">调用此API的API Secret</span></p></td></tr><tr><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">可选</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">tags</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">String</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">包含需要查询的FaceSet标签的字符串，用逗号分隔</span></td></tr><tr><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">可选</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">start</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">Int</span></td><td colspan=\"1\" class=\"confluenceTd\"><p class=\"p1\"><span style=\"color: rgb(0,0,0);\">一个数字 n，表示开始返回的 faceset_token 在传入的 API Key 下的序号。</span></p><p><span style=\"color: rgb(0,0,0);\">通过传入数字 n，可以控制本 API 从第 n 个 faceset_token 开始返回。返回的 faceset_token 按照创建时间排序。每次返回<span class=\"s1\">1000</span>个<span class=\"s1\">FaceSets</span>。</span></p><p class=\"p1\"><span style=\"color: rgb(0,0,0);\">默认值为1。</span></p><p class=\"p1\"><span style=\"color: rgb(255,0,0);\">注意：2017年8月16日后，每次返回的 FaceSet 数量上限从 1000 改为 100。并且您可以输入上一次请求本 API 返回的 next 值，用以获得接下来的 100 个 faceset_token。请尽快修改调整您的程序。</span></p></td></tr></tbody></table></div>\"\"\")[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "e7459b4c",
   "metadata": {},
   "outputs": [],
   "source": [
    "getdetail_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/getdetail\"\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_Secret,\n",
    "    'faceset_token':faceset_token\n",
    "}\n",
    "r = requests.post(url = getdetail_url,params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "35becba5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "6f38afdb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': 'ec1cd2a82d1f83953e6bc146316bd3ef',\n",
       " 'tags': '',\n",
       " 'time_used': 102,\n",
       " 'user_data': '',\n",
       " 'display_name': '',\n",
       " 'face_tokens': [],\n",
       " 'face_count': 0,\n",
       " 'request_id': '1648884546,2623e2b9-aced-4f24-8ad6-5b783377a826',\n",
       " 'outer_id': ''}"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6b4b049b",
   "metadata": {},
   "source": [
    "# 存入face_token数据"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dd15c9e4",
   "metadata": {},
   "source": [
    "# 3.更新数据集描述信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "015becb5",
   "metadata": {},
   "outputs": [],
   "source": [
    "update_FaceSet_url = 'https://api-cn.faceplusplus.com/facepp/v3/faceset/update'\n",
    "\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_Secret,\n",
    "    'faceset_token':faceset_token,\n",
    "    'new_outer_id':'API020305',\n",
    "    'display_name':'20级网新API周二上午班',\n",
    "    'user_data':'59人'\n",
    "}\n",
    "r = requests.post(url=update_FaceSet_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "47379840",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [400]>"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "0b4a6ace",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 98,\n",
       " 'error_message': 'NEW_OUTER_ID_EXIST',\n",
       " 'request_id': '1648884549,6aaa4abf-75d6-4c4e-bec3-f6011c850963'}"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b8b009f3",
   "metadata": {},
   "source": [
    "### 4. 获取所有人脸入数据集合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "1034c958",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "getfacesets_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/getfacesets\"\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_Secret,\n",
    "}\n",
    "r = requests.post(url = getfacesets_url,params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "e8043316",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 103,\n",
       " 'facesets': [{'faceset_token': '2b824a9ec882f9900869ec71b0233654',\n",
       "   'outer_id': 'API020214',\n",
       "   'display_name': '20级网新API周二晚班',\n",
       "   'tags': ''},\n",
       "  {'faceset_token': 'eb4d84e5372e889f054ec4fd61dd5c99',\n",
       "   'outer_id': '',\n",
       "   'display_name': '',\n",
       "   'tags': ''},\n",
       "  {'faceset_token': '04d2396d6f516a6034582be8343def7c',\n",
       "   'outer_id': '',\n",
       "   'display_name': '',\n",
       "   'tags': ''},\n",
       "  {'faceset_token': '909f14bd0322c8f1fa2ab300dd1143eb',\n",
       "   'outer_id': 'API021214',\n",
       "   'display_name': '20级网新API周二晚班',\n",
       "   'tags': ''},\n",
       "  {'faceset_token': 'ff8b86bb8a79b14453aff6b4b6ecfbbb',\n",
       "   'outer_id': '',\n",
       "   'display_name': '',\n",
       "   'tags': ''},\n",
       "  {'faceset_token': '6a1c72e539a97d32dba44c96511eb24f',\n",
       "   'outer_id': 'API020305',\n",
       "   'display_name': '20级网新API周二上午班',\n",
       "   'tags': ''},\n",
       "  {'faceset_token': '5a5cc6709a0ab5d0d243670cffdb8c2e',\n",
       "   'outer_id': '',\n",
       "   'display_name': '',\n",
       "   'tags': ''},\n",
       "  {'faceset_token': '5fe5a07d6e9ad0291e65bef610f2e5a6',\n",
       "   'outer_id': '',\n",
       "   'display_name': '',\n",
       "   'tags': ''},\n",
       "  {'faceset_token': 'cd39ea00a48c32a21e49d0812872412f',\n",
       "   'outer_id': '',\n",
       "   'display_name': '',\n",
       "   'tags': ''},\n",
       "  {'faceset_token': '53d43066eb433042e8fe9d485438c6e1',\n",
       "   'outer_id': '',\n",
       "   'display_name': '',\n",
       "   'tags': ''},\n",
       "  {'faceset_token': '76f87d8a557d1f190b81d0e60f4951e1',\n",
       "   'outer_id': '',\n",
       "   'display_name': '',\n",
       "   'tags': ''},\n",
       "  {'faceset_token': 'acfb85aed17f1fab74af7513a6b29046',\n",
       "   'outer_id': '',\n",
       "   'display_name': '',\n",
       "   'tags': ''},\n",
       "  {'faceset_token': 'ec1cd2a82d1f83953e6bc146316bd3ef',\n",
       "   'outer_id': '',\n",
       "   'display_name': '',\n",
       "   'tags': ''}],\n",
       " 'request_id': '1648884552,754069cb-403a-4003-b93d-3450534dd37d'}"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "78a401af",
   "metadata": {},
   "source": [
    "# 实现所有人脸功能的应用\n",
    "# 作业：\n",
    "* 尝试创建detect.py 并实现功能调用\n",
    "* 尝试创建analyze.py\\campare.py\\search.py，并实现功能调用\n",
    "* 尝试存放宿舍4位同学的人脸信息到 人脸数据集（XXX栋XXX宿舍）:\n",
    "* 完善faceset信息\n",
    "* 实现人脸搜索和人脸对比，打印结果：是本人 OR 不是本人\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0eb44a27",
   "metadata": {},
   "source": [
    "## 1.实现人脸检测获取face_token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "10d167aa",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "API_key = 'b0EHP47Q3skXaAdxRUxVSPdsDFpQe93z'\n",
    "API_Secret = 'ep0nGCCMAqSrGeXMx7202O6Xy7MDC1k7'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "3dc743b7",
   "metadata": {},
   "outputs": [],
   "source": [
    "import detect"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "0ce15089",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on module detect:\n",
      "\n",
      "NAME\n",
      "    detect\n",
      "\n",
      "FUNCTIONS\n",
      "    face_detect(API_key, API_Secret, image_path)\n",
      "        该函数为调用face++ face_detect接口\n",
      "\n",
      "FILE\n",
      "    c:\\users\\97044\\desktop\\大学作业\\大二下作业\\api\\week04\\detect.py\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(detect)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "2e298bca",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648884556,0b697aae-8c3d-43d6-a63e-0577cf57317d',\n",
       " 'time_used': 256,\n",
       " 'faces': [{'face_token': '7760d5cc1e9d122ad6f5124c11d7b474',\n",
       "   'face_rectangle': {'top': 140, 'left': 52, 'width': 140, 'height': 140}}],\n",
       " 'image_id': 'qn9K1bXi+XB7ybzlpomhHw==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "detect.face_detect(API_key,API_Secret,'qianxi.jpg' )"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dcf7e8ba",
   "metadata": {},
   "source": [
    "# 人脸分析"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "c0c1773f",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "API_key = 'b0EHP47Q3skXaAdxRUxVSPdsDFpQe93z'\n",
    "API_Secret = 'ep0nGCCMAqSrGeXMx7202O6Xy7MDC1k7'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "acec1181",
   "metadata": {},
   "outputs": [],
   "source": [
    "import analyze"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "562132c1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on module analyze:\n",
      "\n",
      "NAME\n",
      "    analyze\n",
      "\n",
      "FUNCTIONS\n",
      "    face_analyze(API_key, API_Secret, face_tokens, attributes=None, landmark=1)\n",
      "        该函数为调用face++ face_detect接口\n",
      "\n",
      "FILE\n",
      "    c:\\users\\97044\\desktop\\大学作业\\大二下作业\\api\\week04\\analyze.py\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(analyze)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "9f08c087",
   "metadata": {},
   "outputs": [],
   "source": [
    "face1 = detect.face_detect(API_key,API_Secret,'qianxi.jpg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "a9b3759e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'02491bd1304d9d5b09ffff871613c023'"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_tokens1 = face1[\"faces\"][0][\"face_token\"]\n",
    "face_tokens1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "fc3cd569",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 77,\n",
       " 'request_id': '1648884560,c5efaba8-c1fd-4141-946f-af19d7c9d462',\n",
       " 'faces': [{'landmark': {'mouth_upper_lip_left_contour2': {'y': 233, 'x': 101},\n",
       "    'mouth_upper_lip_top': {'y': 227, 'x': 116},\n",
       "    'mouth_upper_lip_left_contour1': {'y': 227, 'x': 110},\n",
       "    'left_eye_upper_left_quarter': {'y': 171, 'x': 70},\n",
       "    'left_eyebrow_lower_middle': {'y': 157, 'x': 72},\n",
       "    'mouth_upper_lip_left_contour3': {'y': 238, 'x': 106},\n",
       "    'right_eye_top': {'y': 157, 'x': 141},\n",
       "    'left_eye_bottom': {'y': 177, 'x': 78},\n",
       "    'right_eyebrow_lower_left_quarter': {'y': 147, 'x': 128},\n",
       "    'right_eye_pupil': {'y': 161, 'x': 141},\n",
       "    'mouth_lower_lip_right_contour1': {'y': 235, 'x': 130},\n",
       "    'mouth_lower_lip_right_contour3': {'y': 244, 'x': 129},\n",
       "    'mouth_lower_lip_right_contour2': {'y': 239, 'x': 136},\n",
       "    'contour_chin': {'y': 281, 'x': 127},\n",
       "    'contour_left9': {'y': 281, 'x': 109},\n",
       "    'left_eye_lower_right_quarter': {'y': 175, 'x': 85},\n",
       "    'mouth_lower_lip_top': {'y': 237, 'x': 118},\n",
       "    'right_eyebrow_upper_middle': {'y': 137, 'x': 137},\n",
       "    'left_eyebrow_left_corner': {'y': 160, 'x': 54},\n",
       "    'right_eye_bottom': {'y': 166, 'x': 142},\n",
       "    'contour_left7': {'y': 268, 'x': 81},\n",
       "    'contour_left6': {'y': 256, 'x': 71},\n",
       "    'contour_left5': {'y': 242, 'x': 64},\n",
       "    'contour_left4': {'y': 227, 'x': 59},\n",
       "    'contour_left3': {'y': 213, 'x': 54},\n",
       "    'contour_left2': {'y': 198, 'x': 49},\n",
       "    'contour_left1': {'y': 183, 'x': 47},\n",
       "    'left_eye_lower_left_quarter': {'y': 177, 'x': 72},\n",
       "    'contour_right1': {'y': 158, 'x': 183},\n",
       "    'contour_right3': {'y': 190, 'x': 189},\n",
       "    'contour_right2': {'y': 174, 'x': 187},\n",
       "    'mouth_left_corner': {'y': 242, 'x': 95},\n",
       "    'contour_right4': {'y': 207, 'x': 190},\n",
       "    'contour_right7': {'y': 255, 'x': 175},\n",
       "    'right_eyebrow_left_corner': {'y': 147, 'x': 118},\n",
       "    'nose_right': {'y': 205, 'x': 132},\n",
       "    'nose_tip': {'y': 199, 'x': 110},\n",
       "    'contour_right5': {'y': 225, 'x': 188},\n",
       "    'nose_contour_lower_middle': {'y': 213, 'x': 113},\n",
       "    'left_eyebrow_lower_left_quarter': {'y': 159, 'x': 62},\n",
       "    'mouth_lower_lip_left_contour3': {'y': 247, 'x': 110},\n",
       "    'right_eye_right_corner': {'y': 161, 'x': 154},\n",
       "    'right_eye_lower_right_quarter': {'y': 164, 'x': 149},\n",
       "    'mouth_upper_lip_right_contour2': {'y': 228, 'x': 134},\n",
       "    'right_eyebrow_lower_right_quarter': {'y': 144, 'x': 148},\n",
       "    'left_eye_left_corner': {'y': 176, 'x': 66},\n",
       "    'mouth_right_corner': {'y': 234, 'x': 143},\n",
       "    'mouth_upper_lip_right_contour3': {'y': 234, 'x': 130},\n",
       "    'right_eye_lower_left_quarter': {'y': 166, 'x': 135},\n",
       "    'left_eyebrow_right_corner': {'y': 151, 'x': 90},\n",
       "    'left_eyebrow_lower_right_quarter': {'y': 155, 'x': 81},\n",
       "    'right_eye_center': {'y': 163, 'x': 141},\n",
       "    'nose_left': {'y': 211, 'x': 94},\n",
       "    'mouth_lower_lip_left_contour1': {'y': 239, 'x': 107},\n",
       "    'left_eye_upper_right_quarter': {'y': 169, 'x': 84},\n",
       "    'right_eyebrow_lower_middle': {'y': 145, 'x': 138},\n",
       "    'left_eye_top': {'y': 168, 'x': 77},\n",
       "    'left_eye_center': {'y': 173, 'x': 78},\n",
       "    'contour_left8': {'y': 277, 'x': 94},\n",
       "    'contour_right9': {'y': 276, 'x': 146},\n",
       "    'right_eye_left_corner': {'y': 166, 'x': 128},\n",
       "    'mouth_lower_lip_bottom': {'y': 247, 'x': 120},\n",
       "    'left_eyebrow_upper_left_quarter': {'y': 153, 'x': 60},\n",
       "    'left_eye_pupil': {'y': 172, 'x': 79},\n",
       "    'right_eyebrow_upper_left_quarter': {'y': 140, 'x': 126},\n",
       "    'contour_right8': {'y': 267, 'x': 162},\n",
       "    'right_eyebrow_right_corner': {'y': 143, 'x': 159},\n",
       "    'right_eye_upper_left_quarter': {'y': 160, 'x': 133},\n",
       "    'left_eyebrow_upper_middle': {'y': 149, 'x': 70},\n",
       "    'right_eyebrow_upper_right_quarter': {'y': 138, 'x': 148},\n",
       "    'nose_contour_left1': {'y': 172, 'x': 97},\n",
       "    'nose_contour_left2': {'y': 198, 'x': 96},\n",
       "    'mouth_upper_lip_right_contour1': {'y': 224, 'x': 123},\n",
       "    'nose_contour_right1': {'y': 168, 'x': 118},\n",
       "    'nose_contour_right2': {'y': 194, 'x': 126},\n",
       "    'mouth_lower_lip_left_contour2': {'y': 245, 'x': 103},\n",
       "    'contour_right6': {'y': 241, 'x': 184},\n",
       "    'nose_contour_right3': {'y': 210, 'x': 123},\n",
       "    'nose_contour_left3': {'y': 213, 'x': 103},\n",
       "    'left_eye_right_corner': {'y': 173, 'x': 91},\n",
       "    'left_eyebrow_upper_right_quarter': {'y': 148, 'x': 80},\n",
       "    'right_eye_upper_right_quarter': {'y': 158, 'x': 148},\n",
       "    'mouth_upper_lip_bottom': {'y': 235, 'x': 118}},\n",
       "   'face_rectangle': {'width': 140, 'top': 140, 'left': 52, 'height': 140},\n",
       "   'face_token': '02491bd1304d9d5b09ffff871613c023'}]}"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "analyze.face_analyze(API_key,API_Secret,face_tokens1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d30fe139",
   "metadata": {},
   "source": [
    "# 人脸对比（功能）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "75539078",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "API_key = 'b0EHP47Q3skXaAdxRUxVSPdsDFpQe93z'\n",
    "API_Secret = 'ep0nGCCMAqSrGeXMx7202O6Xy7MDC1k7'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "107667a9",
   "metadata": {},
   "outputs": [],
   "source": [
    "import compare"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "61140af7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on module compare:\n",
      "\n",
      "NAME\n",
      "    compare\n",
      "\n",
      "FUNCTIONS\n",
      "    face_compare(API_key, API_secret, face_token1, face_token2)\n",
      "        该函数为调用face++ face_detect接口\n",
      "\n",
      "FILE\n",
      "    c:\\users\\97044\\desktop\\大学作业\\大二下作业\\api\\week04\\compare.py\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(compare)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "ce927f35",
   "metadata": {},
   "outputs": [],
   "source": [
    "face01 = detect.face_detect(API_key,API_Secret,'qianxi.jpg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "c1c98a25",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'81556e9aaab69552b261535541c0a9e6'"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_token01 = face01[\"faces\"][0][\"face_token\"]\n",
    "face_token01"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "fc4a3e2d",
   "metadata": {},
   "outputs": [],
   "source": [
    "face02 = detect.face_detect(API_key,API_Secret,'qianxi02.jpg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "9f8faf16",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'d0c1d3f11e11830990d11d0973c4849b'"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_token02 = face02[\"faces\"][0][\"face_token\"]\n",
    "face_token02"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "15ef3e89",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648887522,6580e40f-345a-4206-b2bf-9120d7346a5e',\n",
       " 'time_used': 370,\n",
       " 'confidence': 68.746,\n",
       " 'thresholds': {'1e-3': 62.327, '1e-4': 69.101, '1e-5': 73.975}}"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "compare.face_compare(API_key,API_Secret,face_token01,face_token02)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ad20eb4b",
   "metadata": {},
   "source": [
    "# 获取人脸集合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c52933fd",
   "metadata": {},
   "outputs": [],
   "source": [
    "getfacesets_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/getfacesets\"\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_Secret,\n",
    "}\n",
    "r = requests.post(url = getfacesets_url,params = payload)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ae3e5637",
   "metadata": {},
   "source": [
    "# 人脸搜索"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "e5735d35",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "API_key = 'b0EHP47Q3skXaAdxRUxVSPdsDFpQe93z'\n",
    "API_Secret = 'ep0nGCCMAqSrGeXMx7202O6Xy7MDC1k7'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "bfc21646",
   "metadata": {},
   "outputs": [],
   "source": [
    "import search"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "8d43939d",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on module search:\n",
      "\n",
      "NAME\n",
      "    search\n",
      "\n",
      "FUNCTIONS\n",
      "    face_search(API_key, API_Secret, face_token, faceset_token)\n",
      "        该函数为调用face++ face_compare接口\n",
      "\n",
      "FILE\n",
      "    c:\\users\\97044\\desktop\\大学作业\\大二下作业\\api\\week04\\search.py\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(search)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "570ae07d",
   "metadata": {},
   "outputs": [],
   "source": [
    "face = detect.face_detect(API_key,API_Secret,'qianxi.jpg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "e26858d8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'edb2259fca555bec8fd85fda168d9c5b'"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_token = face[\"faces\"][0][\"face_token\"]\n",
    "face_token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "939f0edf",
   "metadata": {},
   "outputs": [],
   "source": [
    "getfacesets_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/getfacesets\"\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_Secret,\n",
    "}\n",
    "r = requests.post(url = getfacesets_url,params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "49154ff0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'ec1cd2a82d1f83953e6bc146316bd3ef'"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "faceset_token = r.json()[\"facesets\"][-1][\"faceset_token\"]\n",
    "faceset_token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "6fbcf440",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 103,\n",
       " 'error_message': 'EMPTY_FACESET',\n",
       " 'request_id': '1648887601,25323e6e-420e-41f6-92a6-38af04f2514f'}"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "search.face_search(API_key,API_Secret,face_token,faceset_token)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "757e54fe",
   "metadata": {},
   "source": [
    "# 尝试存放宿舍4位同学的人脸信息到信息集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "eb7308d3",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "API_key = 'b0EHP47Q3skXaAdxRUxVSPdsDFpQe93z'\n",
    "API_Secret = 'ep0nGCCMAqSrGeXMx7202O6Xy7MDC1k7'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "894ac85f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 81,\n",
       " 'error_message': 'FACESET_EXIST',\n",
       " 'request_id': '1648888609,6c082aad-0f7d-4854-862e-34ac743016c4'}"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建人脸集合\n",
    "create_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/create\"\n",
    "payload = {\n",
    "    \"api_key\":API_key,\n",
    "    \"api_secret\":API_Secret,\n",
    "    \"display_name\":\"东16-101\",\n",
    "    \"outer_id\":\"4人\"\n",
    "}\n",
    "r = requests.post(create_url,params = payload)\n",
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "e6012da6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 121,\n",
       " 'facesets': [{'faceset_token': '2b824a9ec882f9900869ec71b0233654',\n",
       "   'outer_id': 'API020214',\n",
       "   'display_name': '20级网新API周二晚班',\n",
       "   'tags': ''},\n",
       "  {'faceset_token': 'eb4d84e5372e889f054ec4fd61dd5c99',\n",
       "   'outer_id': '',\n",
       "   'display_name': '',\n",
       "   'tags': ''},\n",
       "  {'faceset_token': '04d2396d6f516a6034582be8343def7c',\n",
       "   'outer_id': '',\n",
       "   'display_name': '',\n",
       "   'tags': ''},\n",
       "  {'faceset_token': '909f14bd0322c8f1fa2ab300dd1143eb',\n",
       "   'outer_id': 'API021214',\n",
       "   'display_name': '20级网新API周二晚班',\n",
       "   'tags': ''},\n",
       "  {'faceset_token': 'ff8b86bb8a79b14453aff6b4b6ecfbbb',\n",
       "   'outer_id': '',\n",
       "   'display_name': '',\n",
       "   'tags': ''},\n",
       "  {'faceset_token': '6a1c72e539a97d32dba44c96511eb24f',\n",
       "   'outer_id': 'API020305',\n",
       "   'display_name': '20级网新API周二上午班',\n",
       "   'tags': ''},\n",
       "  {'faceset_token': '5a5cc6709a0ab5d0d243670cffdb8c2e',\n",
       "   'outer_id': '',\n",
       "   'display_name': '',\n",
       "   'tags': ''},\n",
       "  {'faceset_token': '5fe5a07d6e9ad0291e65bef610f2e5a6',\n",
       "   'outer_id': '',\n",
       "   'display_name': '',\n",
       "   'tags': ''},\n",
       "  {'faceset_token': 'cd39ea00a48c32a21e49d0812872412f',\n",
       "   'outer_id': '',\n",
       "   'display_name': '',\n",
       "   'tags': ''},\n",
       "  {'faceset_token': '53d43066eb433042e8fe9d485438c6e1',\n",
       "   'outer_id': '',\n",
       "   'display_name': '',\n",
       "   'tags': ''},\n",
       "  {'faceset_token': '76f87d8a557d1f190b81d0e60f4951e1',\n",
       "   'outer_id': '',\n",
       "   'display_name': '',\n",
       "   'tags': ''},\n",
       "  {'faceset_token': 'acfb85aed17f1fab74af7513a6b29046',\n",
       "   'outer_id': '',\n",
       "   'display_name': '',\n",
       "   'tags': ''},\n",
       "  {'faceset_token': 'ec1cd2a82d1f83953e6bc146316bd3ef',\n",
       "   'outer_id': '',\n",
       "   'display_name': '',\n",
       "   'tags': ''},\n",
       "  {'faceset_token': 'ce501d61bcf4f7eec5d990366d7101bc',\n",
       "   'outer_id': '4人',\n",
       "   'display_name': '东16-101',\n",
       "   'tags': ''}],\n",
       " 'request_id': '1648888713,703cee2d-23fe-4f06-ac13-44adb31fc549'}"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获取人脸库列表及信息\n",
    "getfaceset_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/getfacesets\"\n",
    "payload = {\n",
    "    \"api_key\":API_key,\n",
    "    \"api_secret\":API_Secret\n",
    "}\n",
    "r = requests.post(getfaceset_url,params = payload)\n",
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "885bfc23",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'ce501d61bcf4f7eec5d990366d7101bc'"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获取faceset_token\n",
    "faceset_token = r.json()[\"facesets\"][-1][\"faceset_token\"]\n",
    "faceset_token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "3e542f73",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': 'ce501d61bcf4f7eec5d990366d7101bc',\n",
       " 'request_id': '1648889655,83984de7-6ba9-44dd-b91a-7a3a5b1f108a',\n",
       " 'time_used': 85,\n",
       " 'outer_id': '4人'}"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 更新信息\n",
    "update_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/update\"\n",
    "payload = {\n",
    "    \"api_key\":API_key,\n",
    "    \"api_secret\":API_Secret,\n",
    "    \"faceset_token\":faceset_token,\n",
    "    \"user_data\":\"101\"\n",
    "}\n",
    "r = requests.post(update_url,params = payload)\n",
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "id": "b354b35b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': 'ce501d61bcf4f7eec5d990366d7101bc',\n",
       " 'tags': '',\n",
       " 'time_used': 100,\n",
       " 'user_data': '101',\n",
       " 'display_name': '东16-101',\n",
       " 'face_tokens': [],\n",
       " 'face_count': 0,\n",
       " 'request_id': '1648890053,309cab71-7ee7-473f-a73b-02599f028ea9',\n",
       " 'outer_id': '4人'}"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 检查数据集合\n",
    "getDetail_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/getdetail\"\n",
    "\n",
    "payload = {\n",
    "    \"api_key\":API_key,\n",
    "    \"api_secret\":API_Secret,\n",
    "    \"faceset_token\":faceset_token,\n",
    "}\n",
    "\n",
    "r = requests.post(getDetail_url, payload)\n",
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "id": "fa4313d7",
   "metadata": {},
   "outputs": [],
   "source": [
    "import detect,addface,compare,analyze,search"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "id": "05715e98",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'522148ca8585365a77d56e310fa806c7'"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 将图片1存入\n",
    "first = detect.face_detect(API_key,API_Secret,\"1.jpg\")\n",
    "face01_token = first[\"faces\"][0][\"face_token\"]\n",
    "face01_token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "id": "c66073b3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': 'ce501d61bcf4f7eec5d990366d7101bc',\n",
       " 'time_used': 518,\n",
       " 'face_count': 1,\n",
       " 'face_added': 1,\n",
       " 'request_id': '1648890178,73a46128-4dd0-47d9-833b-a617bc91ed0d',\n",
       " 'outer_id': '4人',\n",
       " 'failure_detail': []}"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 将人脸添加到集合\n",
    "addface.addFace(API_key,API_Secret,faceset_token, face01_token)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "id": "861d9699",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'670ca269a9ed51d826234ab7760e502e'"
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 将图片2存入\n",
    "sed = detect.face_detect(API_key,API_Secret,\"2.jpg\")\n",
    "face02_token = sed[\"faces\"][0][\"face_token\"]\n",
    "face02_token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "id": "41c7d670",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': 'ce501d61bcf4f7eec5d990366d7101bc',\n",
       " 'time_used': 452,\n",
       " 'face_count': 4,\n",
       " 'face_added': 1,\n",
       " 'request_id': '1648890289,b6b867e4-b89c-45f9-8b26-13596f8abfc2',\n",
       " 'outer_id': '4人',\n",
       " 'failure_detail': []}"
      ]
     },
     "execution_count": 81,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 将人脸添加到集合\n",
    "addface.addFace(API_key,API_Secret,faceset_token, face02_token)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "id": "dfd8cd11",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'1efda616a53fe55a2e39b7f6b7f2ac02'"
      ]
     },
     "execution_count": 82,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 将图片3存入\n",
    "third = detect.face_detect(API_key,API_Secret,\"3.jpg\")\n",
    "face03_token = third[\"faces\"][0][\"face_token\"]\n",
    "face03_token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "id": "b16724d9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': 'ce501d61bcf4f7eec5d990366d7101bc',\n",
       " 'time_used': 362,\n",
       " 'face_count': 5,\n",
       " 'face_added': 1,\n",
       " 'request_id': '1648890305,b0f0e530-cbf7-4a9c-a132-ddab3d66a82b',\n",
       " 'outer_id': '4人',\n",
       " 'failure_detail': []}"
      ]
     },
     "execution_count": 83,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 将人脸添加到集合\n",
    "addface.addFace(API_key,API_Secret,faceset_token, face03_token)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "id": "2becb4ed",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'8d918a081bfb535126ce70844590d357'"
      ]
     },
     "execution_count": 85,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 将图片4存入\n",
    "four = detect.face_detect(API_key,API_Secret,\"4.jpg\")\n",
    "face04_token = four[\"faces\"][0][\"face_token\"]\n",
    "face04_token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "id": "a164e444",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': 'ce501d61bcf4f7eec5d990366d7101bc',\n",
       " 'time_used': 645,\n",
       " 'face_count': 6,\n",
       " 'face_added': 1,\n",
       " 'request_id': '1648890341,20222e75-deca-44aa-ac3a-a3046e59a604',\n",
       " 'outer_id': '4人',\n",
       " 'failure_detail': []}"
      ]
     },
     "execution_count": 86,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 将人脸添加到集合\n",
    "addface.addFace(API_key,API_Secret,faceset_token, face04_token)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0bd3a2ce",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
